package com.yunyike.controller.user;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.model.IdentityCard;
import com.yunyike.model.User;
import com.yunyike.service.IIdentityCardService;
import com.yunyike.utils.CommonUtils;
import com.yunyike.utils.UserUtils;
import com.yunyike.wx.utils.WeiXinUtil;

@Controller
@RequestMapping(value=RequestConstant.USER+RequestConstant.IDENTITYCARD)
public class UserIDCardController extends BaseController {

	private static Logger log = LoggerFactory.getLogger(UserIDCardController.class);
	
	
	@Autowired
	private IIdentityCardService identityCardService;
	/**
	 * 根据用户查询
	 */
	@RequestMapping(value=RequestConstant.FIND_IDENTITYCARD)
	public String findByUser(Model model){
		
		log.info("find IdentityCard by user...");
		
		// 当前用户信息
		User user = getCurrentUser();
		
		IdentityCard idcard = new IdentityCard();
		idcard.setUserId(user.getUserId());
		idcard.setIdentityCardStatus(CommonConstant.NUMBER_1);
		 List<IdentityCard> idcards = identityCardService.selectByIdentityCard(idcard);
		// 收货地址信息
//		if(idcards!=null&&idcards.size()>0){
			model.addAttribute("IdentityCardList", idcards);
			return ResponseConstant.USER_IDCARD_LIST;
//		}else{
//			return ResponseConstant.REDIRECT+RequestConstant.USER+RequestConstant.IDENTITYCARD+RequestConstant.VIEW_IDENTITYCARD+"/0";
//		}
	}
	
	@RequestMapping(value=RequestConstant.VIEW_IDENTITYCARD+"/{id}")
	public String viewIDCard(Model model,@PathVariable String id,HttpServletRequest request,
			HttpServletResponse response){
		log.info("viewIDCard.......,id:"+id);
		
		if(!CommonUtils.isAjax(request)){
			log.info("cart index wx init data...");
			WeiXinUtil.InitData(request, model);
			log.info("cart index wx init data end.");
		}else{
			log.info("the request is get.");
		}
		
		if(StringUtils.isBlank(id)||"0".equals(id)){
			return ResponseConstant.USER_IDCARD_EDIT;
		}
		
		// 当前用户信息
		User user = getCurrentUser();
		IdentityCard idcard = identityCardService.selectByPrimaryKey(id);
		
		if(idcard!=null&&!user.getUserId().equals(idcard.getUserId())){
			log.info("idcard.userId is wrong,not current user");
			idcard = null;
		}
		model.addAttribute("idcard", idcard);
		return ResponseConstant.USER_IDCARD_EDIT;
	}
	
	@RequestMapping(value=RequestConstant.ADD_IDENTITYCARD,method=RequestMethod.POST)
	public String addIDCard(Model model,@Validated IdentityCard idcard ,
			HttpServletResponse response, BindingResult br){
		
		log.info("addIDCard...,idcard:"+idcard);
		
		//checkRequest(br);
		
		// 当前用户信息
		User user = getCurrentUser();
		
		int result = 0;
		
		idcard.setIdentityCardId(UUID.randomUUID().toString());
		idcard.setIsDefault(CommonConstant.NUMBER_0);
		idcard.setUserId(user.getUserId());
		idcard.setIdentityCardStatus(CommonConstant.NUMBER_1);
		result = identityCardService.insert(idcard);
		log.info("addIDCard end.result:"+result);
		return ResponseConstant.REDIRECT+RequestConstant.USER+RequestConstant.IDENTITYCARD+RequestConstant.FIND_IDENTITYCARD;
	}
	
	/**
	 * 删除收货地址
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.DEL_IDENTITYCARD+"/{id}")
	public String removeIDCard(Model model,@PathVariable String id,HttpServletRequest request,
			HttpServletResponse response){
		
		log.info("removeIDCard by id...,id:"+id);
		
		if(StringUtils.isBlank(id)){
			log.error("removeIDCard request param is not correct.");
			return ResponseConstant.FAILED;
		}
		int result = 0;
		// 当前用户信息
		User user = getCurrentUser();
		IdentityCard idcard = identityCardService.selectByPrimaryKey(id);
		
		if(idcard!=null&&!user.getUserId().equals(idcard.getUserId())){
			log.info("idcard.userId is wrong,not current user");
			idcard = null;
		}
		
		if(idcard == null){
			return ResponseConstant.FAILED;
		}else{
			log.info("remove idcard...");
			idcard.setIdentityCardStatus(CommonConstant.NUMBER_0);
			result = identityCardService.updateByPrimaryKeySelective(idcard);
		}
		String sessionId = UserUtils.getSessionId(request, response);
		boolean currentFlag = identityCardService.checkIDCard(idcard, sessionId);
		if(currentFlag){
			identityCardService.chooseIDCard(null, sessionId);
			log.info("SettlementDTO chooseIDCard remove end sessionId="+sessionId);
		}
		
		log.info("remove idcard end.result:"+result);
		
		return ResponseConstant.SUCCESS;
	}
	
	@ResponseBody
	@RequestMapping(value=RequestConstant.DEFAULT_IDENTITYCARD+"/{id}")
	public String setDefaultIDCard(@PathVariable String id){
		
		log.info("set default recepient...,id:"+id);
		
		if(StringUtils.isBlank(id) || "0".equals(id)){
			log.error("request param not correct");
			return ResponseConstant.FAILED;
		}
		int result = 0;
		// 当前用户信息
		User user = getCurrentUser();
		
		IdentityCard idcard = identityCardService.selectByPrimaryKey(id);
		
		if(idcard!=null&&!user.getUserId().equals(idcard.getUserId())){
			log.info("idcard.userId is wrong,not current user");
			idcard = null;
		}
		
		if(idcard == null){
			return ResponseConstant.FAILED;
		}else{
			log.info("set default idcard...");
			idcard.setIsDefault(CommonConstant.NUMBER_1);
			result = identityCardService.setDefaultIdentityCard(idcard);
		}
		
		if(result == CommonConstant.NUMBER_1){
			log.info("set default recepient end.");
			return ResponseConstant.SUCCESS;
		}
		
		log.info("set default recepient end.but result isn't 1. set default recepient failed.");
		return ResponseConstant.FAILED;
		
	}
	@RequestMapping(value=RequestConstant.UPLOAD_IDCARD_IMG)
	public String uploadIDCardImg(Model model,HttpServletRequest request,
			HttpServletResponse response){
		
		return "";
	}
}
